/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.purpose.mapper;

import java.util.List;
import java.util.Map;

import com.koron.purpose.bean.ResponsibilitiesLibBean;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.koron.purpose.bean.JobsDutiesBean;
import com.koron.purpose.domain.JobsDuties;
import com.koron.system.domain.DictData;

public interface JobsDutiesMapper {

	@Insert("insert into jobs_duties(id,add_user,add_time,update_user,update_time,add_user_name,update_user_name,name,corp_code,code,note,serial,isleader,istemplate)"
			+ "values(#{id},#{addUser},#{addTime},#{updateUser},#{updateTime},#{addUserName},#{updateUserName},#{name},#{corpCode},#{code},#{note},#{serial},#{isleader},#{istemplate})")
	int insert(JobsDuties bean);

	@Insert("<script>" +
			"insert into jobs_duties(id,add_user,add_time,update_user,update_time,add_user_name,update_user_name,name,corp_code,code,note,serial,isleader,istemplate) values"
			+"<foreach collection='dutiesList' item='bean' separator =','>"
			+ "(#{bean.id},#{bean.addUser},#{bean.addTime},#{bean.updateUser},#{bean.updateTime},#{bean.addUserName},"
			+"#{bean.updateUserName},#{bean.name},#{bean.corpCode},#{bean.code},#{bean.note},#{bean.serial},#{bean.isleader},#{bean.istemplate})"
			+"</foreach>"
			+ "</script>")
	int batechInsertJobsDuties(@Param("dutiesList") List<JobsDuties> dutiesList);

	@Update("update jobs_duties set update_user=#{updateUser},update_time=#{updateTime},update_user_name=#{updateUserName},"
			+ "name=#{name},code=#{code},note=#{note},serial=#{serial},isleader=#{isleader},istemplate=#{istemplate} where id=#{id}")
	int update(JobsDuties bean);

	@Delete("delete from jobs_duties where id=#{id}")
	int delete(@Param("id") String id);

	@Select("select * from jobs_duties where id=#{id}")
	JobsDutiesBean queryById(@Param("id") String id);

	@Select("<script>"
			+ "select count(*) from jobs_duties where 1=1"
			+ "<if test='codeList!=null and !codeList.isEmpty()'> and corp_code in" +
			"<foreach collection='codeList' item='code' separator =',' open='(' close=')'>" +
			" #{code}</foreach></if>"
			+ "<if test='name!=null and !name.isEmpty()'> and name like #{name} </if>"
			+ " <if test='isleader!=null'> and isleader =#{isleader} </if>"
			+ "</script>")
	int count(@Param("name") String name,@Param("isleader") Integer isleader,@Param("codeList") List<String> codeList);

	@Select("<script>"
			+ "select * from jobs_duties where 1=1 "
			+ "<if test='codeList!=null and !codeList.isEmpty()'> and corp_code in" +
			"<foreach collection='codeList' item='code' separator =',' open='(' close=')'>" +
			" #{code} " +
			"</foreach></if>"
			+ "<if test='name!=null and !name.isEmpty()'> and name like #{name} </if>"
			+ "<if test='isleader!=null'> and isleader =#{isleader} </if>"
			+ "order by serial"
			+ "<if test='start!=null and row!=null'> limit #{start},#{row} </if>"
			+ "</script>")
	List<JobsDutiesBean> list(@Param("name") String name,@Param("codeList") List<String> codeList,@Param("isleader") Integer isleader,@Param("start") Integer start,@Param("row") Integer row);

	@Select("select count(*) from jobs_duties where name=#{name} and corp_code=#{corpCode}")
	Integer addVerification(@Param("name") String name,@Param("corpCode") String corpCode);

	@Select("select count(*) from jobs_duties where name=#{name} and corp_code=#{corpCode} and id!=#{id}")
	Integer updateVerification(@Param("name") String name,@Param("corpCode") String corpCode,@Param("id") String id);

	@Select("<script>"
			+ "select "
			+ "<foreach collection ='ddList' item='item' index='index' separator =','>"
			+ "(CASE b.id WHEN #{item.id} THEN a.note else '' end) as #{item.dictLabel}"
			+ " </foreach>"
			+ "from jobs_duties_task a left join dict_data b on a.task_object_id=b.id"
			+ " where a.duties_id = #{id} and b.dict_key='TaskObject'"
			+ "</script>")
	List<Map<String,Object>> positionMatrix(@Param("ddList") List<DictData> ddList,@Param("id") String id);

	@Select("<script>"
			+ "select j.`name` jobName,k.* from jobs_duties j LEFT JOIN ("
			+ "select a.duties_id as id, "
			+ "<foreach collection ='noteList' item='item' index='index' separator =','>"
			+ "(CASE REPLACE(REPLACE(REPLACE(a.note, CHAR(9), ''), CHAR(32), ''), CHAR(10), '') WHEN #{item} THEN 1 else NULL end) as #{item}"
			+ " </foreach>"
			+ "from jobs_duties_hazard as a )k on j.id=k.id "
			+ " where j.id in "
			+ "<foreach collection='idList' item='id' separator=',' open='(' close=')'> #{id} </foreach>"
			+ "order by j.serial "
			+ "</script>")
	List<Map<String,Object>> hazardMatrix(@Param("noteList") List<String> noteList,@Param("idList") List<String> idList);

	@Insert("<script>" +
			"insert into jobs_duties_responsibilitie(jobs_duties_id, responsibilities_lib_id) VALUES" +
			"<foreach collection='rls' item='bean' separator =','>" +
			"(#{bean.jobsDutiesId}, #{bean.responsibilitiesLibId})" +
			"</foreach>"
			+ "</script>")
	Integer batchInsertRL(@Param("rls") List<Map> rls);

	@Delete("delete from jobs_duties_responsibilitie where jobs_duties_id = #{jobsDutiesId}")
	Integer deleteRLByDutiesId(String jobsDutiesId);

	@Select("select b.* from jobs_duties_responsibilitie a,responsibilities_lib b " +
			"WHERE a.responsibilities_lib_id = b.id and a.jobs_duties_id = #{jobsDutiesId} AND b.`status` = '1'")
	List<ResponsibilitiesLibBean> getResponsibilitiesLibs(String jobsDutiesId);
}
